<script>
	import jsencrypt from '@/components/jsencrypt/jsencrypt.vue';
	export default {
		globalData: {
			server_url: 'http://127.0.0.1:10002/api/Reception',//请求地址
			// server_url: 'http://你的域名/api/Reception',//请求地址
			access_token: '',
			pubKey: '', 
			uniqid: '',
			userid: '', //用户ID
			uuid: '', //设备id
			statusBarHeight: 0, //高度
			requestShowToastCount: 0,
			merchant_id: 0,
			code:""
		},
		onLaunch: function() {
			console.log('App Launch')
			//#ifdef APP-PLUS
			// uni.setStorageSync('user_id',1)
			// uni.clearStorageSync();
			this.check_version();
			this.getUUID();
			//#endif
			this.getCode();
			const that=this;
			uni.$on('access_token', function(data) {
				that.getAdsWxList();
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			
			getUUID: function() {
				//上报设备唯一id
				plus.device.getInfo({
					success: function(e) {
						uni.setStorageSync('uuid', e.uuid)
						let uuid = uni.getStorageSync('uuid');
					},
					fail: function(e) {
						console.log('getDeviceInfo failed: ' + JSON.stringify(e));
					}
				});
			},
			//更新检测
			check_version: function() {
				var server = this.globalData.server_url + "/Update/update"; //检查更新地址
				var req = { //升级检测数据  
					"appid": plus.runtime.appid,
					"version": plus.runtime.version,
					"os": plus.os.name
				};
				uni.request({
					url: server,
					data: req,
					success: (res) => {
						if (res.data.status === 1) {
							var urlStr = encodeURI(res.data.url)
							uni.showModal({ //提醒用户更新  
								title: "更新提示",
								content: res.data.note,
								success: (res) => {
									if (res.confirm) {
										plus.runtime.openURL(urlStr);
									}
								}
							})
						}
					}
				})
			},
			//获取code
			getCode(postUrl, postData, postMethod, doSuccess, doFail, doComplete) {
				const that = this;
				wx.login({
					success(res) {
						console.log(res);
						// return;
						if (res.code) {
							//发起网络请求
							console.log(res.code)
							that.globalData.code = res.code
							that.getToken(postUrl, postData, postMethod, doSuccess, doFail, doComplete);
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			// //获取密钥
			// getpubKey: function(postUrl, postData, postMethod, doSuccess, doFail, doComplete) {
			// 	let that = this;
			// 	let userid = uni.getStorageSync('userid');
			// 	let server = that.globalData.server_url + '/Auth/getpubKey';
			// 	uni.request({
			// 		url: server,
			// 		success: (res) => {
			// 			if (res.data.code == 1) {
			// 				that.globalData.pubKey = res.data.pubKey;
			// 				that.globalData.uniqid = res.data.uniqid
			// 				if (userid) {
			// 					that.getToken(postUrl, postData, postMethod, doSuccess, doFail, doComplete);
			// 				} else {
			// 					that.getTemToken(postUrl, postData, postMethod, doSuccess, doFail, doComplete);
			// 				}
			// 			} else {
			// 				uni.showToast({
			// 					title: '请求出错，请重启应用',
			// 				})
			// 			}
			// 		}
			// 	})
			// },
			// //获取临时凭证
			// getTemToken: function(postUrl, postData, postMethod, doSuccess, doFail, doComplete) {
			// 	let uniqueid = this.globalData.uniqid;
			// 	let publicData = jsencrypt.setEncrypt(this.globalData.pubKey, uniqueid);
			// 	if (uniqueid) {
			// 		uni.request({
			// 			method: "GET",
			// 			url: this.globalData.server_url + "/Auth/createTmpToken",
			// 			data: {
			// 				uniqid: uniqueid,
			// 				publicData: publicData
			// 			},
			// 			success: (res) => {
			// 				const token = res.data.token;
			// 				this.globalData.access_token = token;
			// 				if(postUrl){
			// 					this.request(postUrl, postData, postMethod, doSuccess, doFail, doComplete)
			// 				}
			// 				uni.$emit('access_token', {
			// 					msg: 'access_token更新'
			// 				})
							
			// 			}
			// 		})
			// 	} else {
			// 		uni.request({
			// 			method: "GET",
			// 			url: this.globalData.server_url + "/Auth/createUniqueId",
			// 			success: (res) => {
			// 				const uniqueid = res.data;
			// 				uni.setStorageSync('uniqueid', uniqueid);
			// 				uni.request({
			// 					method: "GET",
			// 					url: this.globalData.server_url + '/Auth/createTmpToken',
			// 					data: {
			// 						uniqueid: uniqueid,
			// 						publicData: publicData
			// 					},
			// 					success: (res) => {
			// 						const token = res.data.token;
			// 						this.globalData.access_token = token;
			// 						if(postUrl){
			// 							this.request(postUrl, postData, postMethod, doSuccess, doFail, doComplete)
			// 						}
			// 						uni.$emit('access_token', {
			// 							msg: 'access_token更新'
			// 						})
			// 					}
			// 				})
			// 			}
			// 		})
			// 	}
			// },
			// //更换正式凭证
			// upToken: function(token) {
			// 	this.globalData.access_token = token;
			// },
			//获取token
			getToken: function(postUrl, postData, postMethod, doSuccess, doFail, doComplete) {
				const that = this;
				uni.request({
					method: "GET",
					url: this.globalData.server_url + '/Auth/getWxToken',
					data: {
						code: that.globalData.code
					},
					success: (res) => {
						const token = res.data.token;
						this.globalData.access_token = token;
						if(postUrl){
							this.request(postUrl, postData, postMethod, doSuccess, doFail, doComplete)
						}
						uni.$emit('access_token', {
							msg: 'access_token更新'
						})
					}
				})
			},
			//基本请求封装
			request: function(postUrl, postData, postMethod, doSuccess, doFail, doComplete, hideToast, noGetToken) {
				if (this.globalData.access_token && this.globalData.access_token != '') {
					var that = this;
					var server_url = that.globalData.server_url;
					var url = new String();
					var data = postData;
					var method = new String();
					var contenType = new String();
			
					//判断请求方式，设置数据类型和请求方法
					if (postMethod == 'GET') {
						contenType = 'application/json';
						method = 'GET';
					} else {
						contenType = 'application/x-www-form-urlencoded';
						method = 'POST';
					}
			
					//判断请求方式，设置请求体和URL
					if (postMethod == 'POST-json') {
						contenType = 'application/json';
						url = server_url + postUrl + '?token=' + this.globalData.access_token;
						data = JSON.stringify(postData);
					} else if (postMethod == 'POST-noToken') {
						url = server_url + postUrl;
					} else {
						url = server_url + postUrl;
						data.token = this.globalData.access_token;
					}
			
					console.log('发起' + method + '请求\n请求地址：' + url + '\n请求参数:');
					var dataStr = JSON.stringify(data);
					var str = dataStr.replace(/\\\"/g, "\"");
					// console.log(str);
					if (hideToast && hideToast == true) {
			
					} else {
						if (this.globalData.requestShowToastCount < 0) {
							this.globalData.requestShowToastCount = 1;
							uni.showLoading({
								title: '数据加载中'
							});
			
						} else {
							this.globalData.requestShowToastCount++;
							uni.showLoading({
								title: '数据加载中'
							});
						}
					}
					uni.request({
						url: url,
						data: data,
						method: method,
						header: {
							'Content-Type': contenType
						},
						success: function(res) {
			
							if (typeof doSuccess == "function") {
								doSuccess(res);
							}
						},
						fail: function(res) {
							if (typeof doFail == "function") {
								doFail(res);
							}
							var data = res.data;
							if (data) {
								uni.showToast({
									icon: "none",
									title: data.msg,
								});
							}
						},
						complete: function(res) {
							var data = res.data;
							if ((data && data.statusCode == 401) || res.statusCode == 401) {
								console.log('重新加载：' + postUrl);
								if (noGetToken && noGetToken == true) {
			
								} else {
									that.getToken(postUrl, postData, postMethod, doSuccess, doFail, doComplete, hideToast);
								}
							}
							if(data.code==4001){
								that.getpubKey(postUrl, postData, postMethod, doSuccess, doFail, doComplete);
							}
							if (typeof doComplete == "function") {
								doComplete(res);
							}
							if (hideToast && hideToast == true) {
			
							} else {
								that.globalData.requestShowToastCount--;
								if (that.globalData.requestShowToastCount <= 0) {
									that.globalData.requestShowToastCount = 0;
									uni.hideLoading();
								}
							}
						}
					});
				} else {
					console.log('token不存在，重新获取');
					this.getCode(postUrl, postData, postMethod, doSuccess, doFail, doComplete);
				}
			},
			//获取站点信息
			getSetting: function() {
				var that=this;
				var settingInfo='';
				var postData = {
					
				}
				//传输信息
				that.request('/Setting/getSetting', postData, 'GET',
					function(res) {
						if (res.data.code == 1) {
							settingInfo=JSON.stringify(res.data.data)
							uni.setStorageSync('settingInfo',settingInfo);
							uni.$emit('updateSetting',{msg:'站点信息更新'})
						}
						console.log('成功');
						console.log(res);
					},
					function(res) {
						console.log('失败');
						console.log(res);
					},
					function(res) {
						console.log('完成');
						console.log(res);
					}
				);
			},
			//获取微信广告列表
			getAdsWxList:function(){
				var that=this;
				var postData = {
					
				}
				//传输信息
				that.request('/Ads/getWxAds', postData, 'GET',
					function(res) {
						if (res.data.code == 1) {
							uni.setStorageSync('AdsWxList',res.data.data);
						}
						console.log('成功');
						console.log(res);
					},
					function(res) {
						console.log('失败');
						console.log(res);
					},
					function(res) {
						console.log('完成');
						console.log(res);
					}
				);
			}
		}

	}
</script>

<style>
/* 解决头条小程序组件内引入字体不生效的问题 */
/* #ifdef MP-TOUTIAO */
@font-face {
	font-family: uniicons;
	src: url('/static/uni.ttf');
}
/* #endif */
</style>
